package com.thread.www.test;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: panjoy
 * @Description:
 * @Date: Created in 10:35 2017-06-14
 * @Modfied By:
 */
public class Client {
    private static class MyThread extends Thread {
        private final DelegatingVehicleTracker vehicleTracker;

        public MyThread(DelegatingVehicleTracker vehicleTracker,String threadName) {
            super(threadName);
            this.vehicleTracker = vehicleTracker;
        }

        @Override
        public void run() {
            for (int i = 9; i >=0; i--) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                vehicleTracker.setLocations(Integer.toString(i), i + 1, i + 1);
            }
            System.out.println(Thread.currentThread().getName() +"------"+ vehicleTracker.getLocations());
        }
    }
    private static class MyThread1 extends Thread {
        private final DelegatingVehicleTracker vehicleTracker;

        public MyThread1(DelegatingVehicleTracker vehicleTracker,String threadName) {
            this.vehicleTracker = vehicleTracker;
        }

        @Override
        public void run() {
            for (int i = 0; i <10; i++) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                vehicleTracker.setLocations(Integer.toString(i), i + 2, i + 2);
            }
            System.out.println(Thread.currentThread().getName() +"------"+ vehicleTracker.getLocations());
        }
    }
    public static void main(String[] args) {
        Map<String, Point> points = new HashMap<String, Point>();
        for (int i = 0; i < 10; i++) {
            Point point = new Point(i + 50, i + 50);
            points.put(Integer.toString(i), point);
        }

        DelegatingVehicleTracker vehicleTracker = new DelegatingVehicleTracker(points);
        new MyThread(vehicleTracker,"thread1---").start();
        new MyThread(vehicleTracker,"thread2---").start();
        new MyThread(vehicleTracker,"thread3---").start();
        new MyThread(vehicleTracker,"thread4---").start();
        new MyThread1(vehicleTracker,"thread5---").start();
        new MyThread1(vehicleTracker,"thread6---").start();
        new MyThread1(vehicleTracker,"thread7---").start();
        new MyThread1(vehicleTracker,"thread8---").start();

    }
}
